#include <iostream>

#include "util.hpp"
#include "config.hpp"
#include "data.hpp"
#include "hot.hpp"
#include "service.hpp"
#include <unistd.h>
#include <thread>

using namespace std;
cloud::DataManager *_data = new cloud::DataManager();

void TestUtil()
{

    cloud::FileUtil sm("./packdir/a.txt.lz");
    cout << sm.Filename() << endl;
    cout << sm.FileSize() << endl;
    cout << sm.LastAtime() << endl;
    cout << sm.LastMtime() << endl;

    // string str, con;
    // sm.GetContent(&str);
    // clould::FileUtil fu("./hello.txt");
    // fu.SetContent(str);

    // sm.Compress("hello.txt.lz");
    sm.UnCompress("./backdir/a.txt");

    // sm.CreateDirectory();
    //  std::vector<std::string> array;
    //  sm.ScanDirectory(&array);
    //  for(auto& path : array)
    //  {
    //      cout << path << endl;
    //  }
}

// void JsonTest()
// {

//     cout << "你是谁啊" <<endl;
//     const char *name = "chian-ocean";
//     int age = 19;
//     float score[] = {85, 88.5, 99};
//     Json::Value root;
//     root["姓名"] = name;
//     root["age"] = age;
//     root["score"].append(score[0]);
//     root["score"].append(score[1]);
//     root["score"].append(score[2]);
//     std::string json_str;
//     cloud::JsonUtil::Serialize(root, &json_str);
//     std::cout << json_str << std::endl;

//     Json::Value val;
//     cloud::JsonUtil::UnSerialize(json_str, &val);
//     std::cout << val["name"].asString() << std::endl;
//     std::cout << val["age"].asString() << std::endl;
// }

void ConfigTest()
{
    cloud::Config *con = cloud::Config::GetInstance();
    cout << "HostTime:  " << con->GetHotTime() << endl;
    cout << "ServerPort:  " << con->GetServerPort() << endl;
    cout << "ServerIPL:  " << con->GetServerIP() << endl;
    cout << "PackFileSuffdix:  " << con->GetPackFileSuffdix() << endl;
    cout << "GetPackFileSuffdix:  " << con->GetPackFileSuffdix() << endl;
    cout << "BackDir:  " << con->GetBackDir() << endl;
    cout << "PackDir:  " << con->GetPackDir() << endl;
    cout << "BackFile:  " << con->GetBackFile() << endl;
}

void Detailtest(cloud::BackupInfo &bup)
{
    cout << "---------- BackupInfo Detail-----------" << endl;
    cout << "pack_flag:  " << bup.pack_flag << endl;
    cout << "atime:  " << bup.atime << endl;
    cout << "mtime:  " << bup.mtime << endl;
    cout << "real_path:  " << bup.real_path << endl;
    cout << "pack_path:  " << bup.pack_path << endl;
    cout << "url:  " << bup.url << endl;
    cout << "---------------- END -----------------" << endl;
}

void DataTest()
{
    // // cloud::BcakupInfo bup;

    // // cout << "pack_flag:  "<< bup.pack_flag <<endl;
    // // cout << "atime:  "<<bup.atime <<endl;
    // // cout << "mtime:  "<< bup.mtime <<endl;s
    // // cout << "real_path:  "<< bup.real_path<<endl;
    // // cout << "pack_path:  "<<bup.pack_path<<endl;
    // // cout << "url:  " <<bup.url<<endl;

    // cloud::DataManager dm;

    // // Detailtest(make);

    // // dm.GetOneByUrl("/download/Makefile",&tmp);
    // // Detailtest(tmp);

    // // cout << "s.real_path:   " << s.real_path << endl;
    // //Detailtest(s);
    // // dm.GetOneByRealpath("./log.hpp",&tmp);s
    // // Detailtest(tmp);

    // dm.Storage();
    cloud::DataManager dm;
    cloud::BcakupInfo make, ss, tmp, s;
    make.NewBackupFile("./Makefile");
    s.NewBackupFile("./log.hpp");
    // Detailtest(s);
    // dm.Insert(s);
    // dm.Insert(make);

    dm.InitLoad();
    vector<cloud::BackupInfo> array;
    dm.Getall(&array);
    for (auto it : array)
        Detailtest(it);
}

void hottest()
{

    _data->InitLoad();
    cloud::HotManager hm;
    hm.RunModel();
}

void Servicetest()
{
    cloud::Service ser;
    ser.RunModel();
}

int main(int argc, char *argv[])
{

    // TestUtil();
    // JsonTest();
    // ConfigTest();
    // DataTest();
    // hottest();


    thread Hot_Manager(hottest);
    thread Server_Manager(Servicetest);
    Hot_Manager.join();
    Server_Manager.join();

    return 0;
}
